﻿Imports System.Data.OleDb
Imports BUS
Imports DTO

Public Class frmGanXe

    Private Sub frmGanXe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cbxTuyenXe.DataSource = TuyenBus.LayDanhSachTuyen()
        cbxTuyenXe.DisplayMember = "TenTuyen"
        cbxTuyenXe.ValueMember = "MaTuyen"

        ' cbxTuyenXe.Text = ""
        'cbxNgayKhoiHanh.Text = ""
        'cbxNgayKhoiHanh.ValueMember = "NgayKhoiHanh"
        ' cbxGioKhoiHanh.Text = ""
        ' cbxGioKhoiHanh.ValueMember = "ThoiDiemKhoiHanh"


    End Sub


    Private Sub cbxTuyenXe_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxTuyenXe.SelectedIndexChanged, cbxTuyenXe.TextChanged
        cbxNgayKhoiHanh.Text = ""
        cbxTuyenXe.ValueMember = "MaTuyen"
        Dim maTuyen As Long = cbxTuyenXe.SelectedValue
        cbxNgayKhoiHanh.DataSource = XL_Chung.TimNgayKhoiHanh(maTuyen)
        cbxNgayKhoiHanh.DisplayMember = "NgayKhoiHanh"

        cbxVe.DataSource = LoaiVeBus.LayDanhSachLoaiVeTheoMaTuyen(maTuyen)
        cbxVe.DisplayMember = "GiaVe"
        cbxVe.ValueMember = "MaLoaiVe"
    End Sub

    Private Sub cbxNgayKhoiHanh_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxNgayKhoiHanh.SelectedIndexChanged, cbxNgayKhoiHanh.TextChanged
        cbxGioKhoiHanh.Text = ""
        cbxTuyenXe.ValueMember = "MaTuyen"
        cbxNgayKhoiHanh.ValueMember = "NgayKhoiHanh"

        Dim maTuyen As Long = cbxTuyenXe.SelectedValue
        Dim ngayKH As Date = cbxNgayKhoiHanh.SelectedValue
        cbxGioKhoiHanh.DataSource = XL_Chung.TimThoiDiemKhoiHanh(maTuyen, ngayKH)
        cbxGioKhoiHanh.DisplayMember = "ThoiDiemKhoiHanh"
    End Sub

    Private Sub HienThiDanhSachXeRanh()
        cbxNgayKhoiHanh.ValueMember = "NgayKhoiHanh"
        Dim NgayKhoiHanh As Date = cbxNgayKhoiHanh.SelectedValue

        cbxGioKhoiHanh.ValueMember = "ThoiDiemKhoiHanh"
        Dim ThoiDiemKhoiHanh As String = cbxGioKhoiHanh.SelectedValue

        dgvDanhSachXeRanh.DataSource = LichGanBus.DanhSachXeDangRanh(NgayKhoiHanh, ThoiDiemKhoiHanh)
    End Sub

    Private Sub cbxGioKhoiHanh_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxGioKhoiHanh.SelectedIndexChanged

        If cbxGioKhoiHanh.Text <> "" Then
            HienThiDanhSachXeRanh()
        End If
    End Sub

    Private Sub btnGanXe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGanXe.Click
        Dim lg As New LichGanDto
        cbxTuyenXe.ValueMember = "MaTuyen"
        lg.MaTuyen = cbxTuyenXe.SelectedValue

        cbxNgayKhoiHanh.ValueMember = "NgayKhoiHanh"
        lg.NgayKhoiHanh = cbxNgayKhoiHanh.SelectedValue

        cbxGioKhoiHanh.ValueMember = "ThoiDiemKhoiHanh"
        lg.ThoiDiemKhoiHanh = cbxGioKhoiHanh.SelectedValue
        If cbxTuyenXe.SelectedValue Is Nothing Then
            MessageBox.Show("Chọn tuyến!!!", "THÔNG BÁO")
            Exit Sub
        ElseIf cbxNgayKhoiHanh.SelectedValue Is Nothing Then
            MessageBox.Show("Chọn ngày!!!", "THÔNG BÁO")
            Exit Sub
        ElseIf cbxGioKhoiHanh.SelectedValue Is Nothing Then
            MessageBox.Show("Chọn giờ!!!", "THÔNG BÁO")
            Exit Sub
        End If


        Dim vitri As Integer
        Try
            vitri = dgvDanhSachXeRanh.CurrentRow.Index
            If vitri = dgvDanhSachXeRanh.Rows.Count - 1 Then
            End If
            lg.MaXe = dgvDanhSachXeRanh.Rows(vitri).Cells("MaXe").Value
        Catch ex As Exception
            MessageBox.Show("Chọn xe cần gán!!!", "THÔNG BÁO")
            Exit Sub
        End Try

        Dim TinhTrangLap = False
        If ckbThuHai.Checked = True Or ckbThuBa.Checked = True Or ckbThuTu.Checked = True _
                                    Or ckbThuNam.Checked = True Or ckbThuSau.Checked = True _
                                    Or ckbThuBay.Checked = True Or ckbChuNhat.Checked = True Then
            TinhTrangLap = True
        End If

        'lg.MaNV = MaNhanVien ???
        Dim kq As Integer = LichGanBus.ThemLichGan(lg)
        If kq <> 0 Then
            Dim maLichGanVuaThem As Long = LichGanBus.LayMaLichGanVuaThem()
            If TinhTrangLap Then
                'Thêm vào bảng lịch gán lặp 
                Dim lglap As New LichGanLapDto
                lglap.MaLichGan = maLichGanVuaThem
                lglap.ThuHai = ckbThuHai.Checked
                lglap.ThuBa = ckbThuBa.Checked
                lglap.ThuTu = ckbThuTu.Checked
                lglap.ThuNam = ckbThuNam.Checked
                lglap.ThuSau = ckbThuSau.Checked
                lglap.ThuBay = ckbThuBay.Checked
                lglap.ChuNhat = ckbChuNhat.Checked

                LichGanLapBus.ThemMotLichGanLap(lglap)
            End If
            ckbThuHai.Checked = False
            ckbThuBa.Checked = False
            ckbThuTu.Checked = False
            ckbThuNam.Checked = False
            ckbThuSau.Checked = False
            ckbThuBay.Checked = False
            ckbChuNhat.Checked = False

            'Tạo danh sách vé
            Dim soChoNgoi As Integer = dgvDanhSachXeRanh.Rows(vitri).Cells("SoChoNgoi").Value
            Dim ve As New VeDto
            ve.MaLich = maLichGanVuaThem
            ve.LoaiVe = cbxVe.SelectedValue
            For i As Integer = 1 To soChoNgoi
                ve.MaGhe = i
                VeBus.ThemVe(ve)
            Next
            MessageBox.Show("Đã gán xe vào tuyến!!!", "THÔNG BÁO")
            HienThiDanhSachXeRanh()
        End If

    End Sub

    Private Sub btnDong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Close()
    End Sub

End Class